<template>
  <ele-modal
    form
    :width="640"
    :model-value="modelValue"
    title="业务员列表"
    @update:modelValue="updateModelValue"
  >
    <ele-pro-table
      ref="tableRef"
      row-key="userId"
      :columns="columns"
      :datasource="datasource"
      :show-overflow-tooltip="true"
      v-model:selections="selections"
      highlight-current-row
    >
      <template #toolbar v-if="props.action === 'list'">
        <el-button
          type="success"
          class="ele-btn-icon"
          :icon="Plus"
          @click="openEdit()"
        >
          新建
        </el-button>
      </template>

      <template #action="{ row }">
        <el-button-group>
          <el-button
            v-if="props.action === 'choose'"
            type="info"
            size="small"
            @click="choose(row)"
          >
            选择
          </el-button>
          <el-button
            v-if="props.action === 'list'"
            type="warning"
            size="small"
            @click="openEdit(row)"
          >
            修改
          </el-button>

          <el-button
            v-if="props.action === 'list'"
            type="danger"
            size="small"
            @click="del(row)"
          >
            删除
          </el-button>
        </el-button-group>
      </template>
    </ele-pro-table>
    <!-- 编辑弹窗 -->
    <edit
      v-model="showEdit"
      :company-id="props.companyId"
      :data="current"
      @done="reload"
    />
  </ele-modal>
</template>

<script setup>
  import { ref, watch } from 'vue';
  import { Plus } from '@element-plus/icons-vue';
  import Edit from './components/edit.vue';
  import {
    pageInsuranceCompanySalesmanList,
    delInsuranceCompanySalesman
  } from '@/api/company';
  import { EleMessage } from 'ele-admin-plus/es';
  import { ElMessageBox } from 'element-plus/es';
  const props = defineProps({
    action: {
      type: String,
      default: 'list'
    },
    /** 弹窗是否打开 */
    modelValue: Boolean,
    /** 修改回显的数据 */
    companyId: Number
  });

  /** 表格实例 */
  const tableRef = ref(null);

  /** 表格列配置 */
  const columns = ref([
    {
      prop: 'id',
      width: 90,
      label: 'ID'
    },
    {
      prop: 'realname',
      label: '名字'
    },
    {
      columnKey: 'action',
      label: '操作',
      minWidth: 100,
      align: 'center',
      slot: 'action',
      fixed: 'right'
    }
  ]);

  /** 表格选中数据 */
  const selections = ref([]);

  /** 当前编辑数据 */
  const current = ref(null);

  /** 是否显示编辑弹窗 */
  const showEdit = ref(false);

  /** 表格数据源 */
  const datasource = ({ page, limit, where, orders }) => {
    return pageInsuranceCompanySalesmanList({
      ...where,
      ...orders,
      page,
      limit,
      insurance_company_id: props.companyId
    });
  };
  const emit = defineEmits(['done', 'update:modelValue']);
  /** 选择企业 */
  const choose = (row) => {
    emit('done', row);
  };

  /** 搜索 */
  const reload = (where) => {
    selections.value = [];
    tableRef.value?.reload?.({ page: 1, where });
  };

  /** 打开编辑弹窗 */
  const openEdit = (row) => {
    current.value = row ?? null;
    showEdit.value = true;
  };

  // 删除
  const del = (row) => {
    ElMessageBox.confirm('确定要删除' + row.title + '吗？', '系统提示', {
      type: 'warning',
      draggable: true
    })
      .then(() => {
        const loading = EleMessage.loading('请求中..');
        delInsuranceCompanySalesman({ id: row.id })
          .then(() => {
            loading.close();
            EleMessage.success('操作成功');
            reload();
          })
          .catch((e) => {
            loading.close();
            EleMessage.error(e.message);
          });
      })
      .catch(() => {});
  };

  /** 更新modelValue */
  const updateModelValue = (value) => {
    emit('update:modelValue', value);
  };

  watch(
    () => props.modelValue,
    (modelValue) => {
      if (modelValue) {
        reload();
      } else {
        console.log('关闭');
      }
    }
  );
</script>
